package com.feng.gaoerfu.view.locktableview;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.feng.gaoerfu.R;
import com.feng.gaoerfu.bean.TableData;
import com.feng.gaoerfu.utils.UIUtils;

import java.util.ArrayList;

/**
 * @time 2017/8/30 0030 10:46
 */

public class TotalViewLayout {

    private Context mContext;
    private ArrayList<ArrayList<TableData>> mTableLists;

    public TotalViewLayout(Context context, ArrayList<ArrayList<TableData>> mTableLists) {
        this.mContext = context;
        this.mTableLists = mTableLists;
    }


    public View initView() {
        LinearLayout linearLayout = new LinearLayout(mContext);
        LinearLayout.LayoutParams textLinearParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.setLayoutParams(textLinearParams);
        for (int i = 0; i < mTableLists.size(); i++) {
            View tableView = getTableView(mTableLists.get(i));

            View line = getLineView();

            linearLayout.addView(tableView);
            linearLayout.addView(line);
        }
        return linearLayout;
    }

    @NonNull
    private View getTableView(ArrayList<TableData> tableDatas) {
        FrameLayout tableView = (FrameLayout) View.inflate(mContext, R.layout.item_table_view, null);
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        layoutParams.height = (int) UIUtils.dp2Px(70);
        layoutParams.width = (int) UIUtils.dp2Px(75);
        tableView.setLayoutParams(layoutParams);

        TextView tvYardNum = (TextView) tableView.findViewById(R.id.tv_table_yard_num);
        LinearLayout layoutHasData = (LinearLayout) tableView.findViewById(R.id.layout_has_data);
        TextView tvGanNum = (TextView) tableView.findViewById(R.id.tv_table_gan_num);
        TextView tvTuiNum = (TextView) tableView.findViewById(R.id.tv_table_tui_num);
        TextView tvErrorNum = (TextView) tableView.findViewById(R.id.tv_table_error);
        int totalNumOfGan = 0;
        int numOfError = 0;
        int numOfTui = 0;
        for (TableData data : tableDatas) {
            totalNumOfGan += data.numOfGan;
            numOfError += data.numOfError;
            numOfTui += data.numOfTui;
        }

        layoutHasData.setVisibility(View.VISIBLE);
        tvYardNum.setVisibility(View.GONE);
        tvGanNum.setText(totalNumOfGan + "");
        tvTuiNum.setText(numOfTui + "");
        tvErrorNum.setText(numOfError + "");
        return tableView;
    }

    private View getLineView() {
        //表格线
        View splite = new View(mContext);
        ViewGroup.LayoutParams spliteLayoutParam = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                DisplayUtil.dip2px(mContext, 1));
        splite.setLayoutParams(spliteLayoutParam);
        splite.setBackgroundColor(ContextCompat.getColor(mContext, R.color.light_gray));
        return splite;
    }

}
